package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.AddCardAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.CreateFuCardBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 卡片赠予
 */
public class AddCardFragment extends BaseFragment {
    public static AddCardFragment newInstance(String mainCardId, String productId) {
        AddCardFragment addCardFragment = new AddCardFragment();
        Bundle bundle = new Bundle();
        bundle.putString("mainCardId", mainCardId);
        bundle.putString("productId", productId);
        addCardFragment.setArguments(bundle);
        return addCardFragment;
    }

    @BindView(R.id.rv_add_card)
    RecyclerView rvAddCard;
    private AddCardAdapter addCardAdapter;
    private String mainCardId;
    private String productId;

    @Override
    protected int initLayout() {
        return R.layout.fg_addcard;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.AddCard), getActivity());
        addCardAdapter = new AddCardAdapter(getContext(), null, 0);
        rvAddCard.setLayoutManager(new LinearLayoutManager(getContext()));
        rvAddCard.setAdapter(addCardAdapter);
        addCardAdapter.setOnClickItemListener((position, num) -> {
            if (num<= 0){
                ToastUtils.showToast(getContext(),"暂无可赠予卡！");
                return;
            }
            String id = addCardAdapter.getListData().get(position).getId();
            start(FillInInformationFragment.newInstance(mainCardId,productId,id));
        });
    }

    @Override
    protected void initData(Context mContext) {
        mainCardId = getArguments().getString("mainCardId");
        productId = getArguments().getString("productId");
    }

    @Override
    protected void loadMethod() {
        getFuCardData();
    }

    private void getFuCardData() {
        RequestUtils.SubCardTypeListByCardId(getContext(), mainCardId, new MyObserver<List<CreateFuCardBean>>(getContext()) {
            @Override
            public void onSuccess(List<CreateFuCardBean> result) {
                addCardAdapter.setListData(result);
                addCardAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(),errorMsg);
            }
        });
    }
}
